﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using Aspose.Cells;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class AdminOrders : System.Web.UI.Page
    {

        public Boolean IsVN = true;
        private String languageCode = "vi-VN";     
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private Int32 orderSate = HangSo.DEFAULT_ID;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 20;
        private Int32 totalRecord;      
        private Cells _range;
        private Worksheet _exSheet;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] == null)
                    Response.Redirect("../Account/EHTLogin.aspx");
                else
                {
                    try
                    {
                        languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                        if (languageCode == "en-us")
                        {
                            IsVN = false;
                        }

                    }
                    catch (Exception ex)
                    {
                    }

                    try
                    {
                        orderSate = Int32.Parse(Request.QueryString["OrderState"].ToString());
                    }
                    catch (Exception ex)
                    {
                        orderSate = HangSo.DEFAULT_ID;
                    }

                    try
                    {
                        pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
                    }
                    catch (Exception)
                    {
                        pageIndex = 1;
                    }

                    Int32 totalOrderAppending = 0;// tổng số các order đang đợi để ship
                    Int32 totalOrderShipping = 0; // tổng số các order đang ship
                    //lấy thông số các order
                    OrderProvider.GetParametersOfOrder(out totalOrderAppending, out totalOrderShipping);
                    this.hlOrderAppending.Text = "Bestill Føye (" + totalOrderAppending.ToString() + ")";
                    this.hlOrderShipping.Text = "Bestill Skipsfart (" + totalOrderShipping.ToString() + ")";
                    //load dữ liệu order theo trạng thái
                    LoadDataOrder(orderSate);

                    Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                    LoadLanguage(Thread.CurrentThread.CurrentCulture);
                }
                
            }
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbOrder.Text = rm.GetString("lbOrder", ci);            

        }

        /// <summary>
        /// ham load order theo trang thai cua order
        /// </summary>
        /// <param name="orderState"></param>
        private void LoadDataOrder(Int32 orderState)
        {
            DataSet dsOrders = OrderProvider.GetOrdersByOrderState(orderState, pageIndex, pageSize, out totalRecord);
            this.rpOrders.DataSource = dsOrders;
            this.rpOrders.DataBind();

            this.PaggingControl1.PageURL = "AdminOrders.aspx?OrderState=" + orderState.ToString();
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PagingSize = pageSize;
        }


        // ham fill du lieu vao excel
        protected void FillData(object value, int row, int column)
        {
            try
            {
                _exSheet.Cells[row - 1, column - 1].PutValue(value);
            }
            catch (Exception e)
            {

            }
        }

        protected void lbtExport_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet dsOrdersExport = OrderProvider.GetOrdersExportCVS();

                DataTable dtOrders = dsOrdersExport.Tables[0];

                // chuẩn bị tệp ghi dữ liêu
                string excelFile = Server.MapPath("~/ExcelTem/ListOrder.xls");
                Workbook exBook = new Workbook();
                exBook.Open(excelFile, FileFormatType.Excel2003);
                _exSheet = exBook.Worksheets[0];
                _range = _exSheet.Cells;
                // Ghi du lieu vao file excel
                int i = 5;
                FillData("LISTE ORDER", 2, 2);

                for (int j = 0; j < dtOrders.Rows.Count - 1; j++)
                {
                    FillData(dtOrders.Rows[j]["ReceivedName"], i, 1);
                    FillData(dtOrders.Rows[j]["ReceivedAddress"], i, 2);
                    FillData(dtOrders.Rows[j]["ReceivedCity"], i, 3);
                    FillData(dtOrders.Rows[j]["ReceicedPhone"], i, 4);
                    i++;
                }
                exBook.Save("ListOrder.xls", SaveType.OpenInExcel, FileFormatType.Default, this.Response);
            }

            catch (Exception ex)
            {
                Response.Redirect("AdminOrders.aspx");
            }
        }
    }
}